using Microsoft.EntityFrameworkCore;
using PandaWebAPI.Models.ef;

namespace PandaWebAPI.Services
{
    public class PandaBasisService : BaseService<PandaBasis>, IPandaBasisService
    {
        public PandaBasisService(PandaSystemContext context) : base(context)
        {
        }

        public async Task<List<PandaBasis>> GetBasesByLocationAsync(string location)
        {
            return await _dbSet
                .Include(b => b.Panda)
                .Where(b => b.Location.Contains(location))
                .OrderBy(b => b.Name)
                .ToListAsync();
        }

        public async Task<PandaBasis> UpdateBaseImageAsync(Guid baseId, string imageUrl)
        {
            var basis = await _dbSet.FindAsync(baseId);
            if (basis == null)
                throw new KeyNotFoundException($"Panda base with ID {baseId} not found.");

            basis.ImageUrl = imageUrl;
            await _context.SaveChangesAsync();

            return basis;
        }

        public async Task<PandaBasis> UpdateWebsiteUrlAsync(Guid baseId, string websiteUrl)
        {
            var basis = await _dbSet.FindAsync(baseId);
            if (basis == null)
                throw new KeyNotFoundException($"Panda base with ID {baseId} not found.");

            basis.WebsiteUrl = websiteUrl;
            await _context.SaveChangesAsync();

            return basis;
        }

        public override async Task<PandaBasis> CreateAsync(PandaBasis basis)
        {
            // 设置默认值
            basis.BaseId = Guid.NewGuid();

            // 验证必填字段
            if (string.IsNullOrWhiteSpace(basis.Name))
                throw new ArgumentException("Base name is required.");
            if (string.IsNullOrWhiteSpace(basis.Location))
                throw new ArgumentException("Base location is required.");

            await _dbSet.AddAsync(basis);
            await _context.SaveChangesAsync();

            return basis;
        }

        public override async Task<PandaBasis> UpdateAsync(PandaBasis basis)
        {
            var existingBasis = await _dbSet.FindAsync(basis.BaseId);
            if (existingBasis == null)
                throw new KeyNotFoundException($"Panda base with ID {basis.BaseId} not found.");

            // 更新字段
            existingBasis.Name = basis.Name;
            existingBasis.Location = basis.Location;
            existingBasis.WebsiteUrl = basis.WebsiteUrl;
            existingBasis.Description = basis.Description;
            existingBasis.ImageUrl = basis.ImageUrl;

            // 验证必填字段
            if (string.IsNullOrWhiteSpace(existingBasis.Name))
                throw new ArgumentException("Base name is required.");
            if (string.IsNullOrWhiteSpace(existingBasis.Location))
                throw new ArgumentException("Base location is required.");

            await _context.SaveChangesAsync();

            return existingBasis;
        }

        public override async Task<bool> DeleteAsync(Guid id)
        {
            var basis = await _dbSet
                .Include(b => b.Panda)
                .FirstOrDefaultAsync(b => b.BaseId == id);

            if (basis == null)
                return false;

            // 检查是否有关联的熊猫
            if (basis.Panda.Any())
                throw new InvalidOperationException("Cannot delete base with associated pandas.");

            _dbSet.Remove(basis);
            await _context.SaveChangesAsync();

            return true;
        }
    }
} 